package com.vision.sales.customer.cl.ui;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.vision.sales.cm.db.data.AdvancePayment;
import com.vision.sales.cm.db.data.AppliedAdvancePayment;

/**
 * 
 * @author Mark
 *
 * @param 
 */
public interface AdvancePaymentsTreeNodeProperties extends PropertyAccess<AdvancePaymentsTreeNode> {
	
	public static final AdvancePaymentsTreeNodeProperties INSTANCE = GWT.create(AdvancePaymentsTreeNodeProperties.class);
	
	final ModelKeyProvider<AdvancePaymentsTreeNode> key = new ModelKeyProvider<AdvancePaymentsTreeNode>() {
		@Override
		public String getKey(AdvancePaymentsTreeNode item) {
			if (item.getObject() instanceof AdvancePayment) {
				AdvancePayment a = (AdvancePayment) item.getObject();
				return "ap-" + a.getId();
			}
			
			AppliedAdvancePayment a = (AppliedAdvancePayment) item.getObject();
			return "aap-" + a.getId();
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, String> advancePaymentNo = new ValueProvider<AdvancePaymentsTreeNode, String>() {
		@Override
		public String getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AdvancePayment) {
				AdvancePayment a = (AdvancePayment) model.getObject();
				return a.getAdvancePaymentNo();
			}
			return "";
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, String value) {
			
		}
		@Override
		public String getPath() {
			return "advancePayment.advancePaymentNo";
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, Date> advancePaymentDate = new ValueProvider<AdvancePaymentsTreeNode, Date>() {
		@Override
		public Date getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AdvancePayment) {
				AdvancePayment a = (AdvancePayment) model.getObject();
				return a.getDate();
			}
			return null;
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, Date value) {
			
		}
		@Override
		public String getPath() {
			return "advancePayment.date";
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, Double> advancePaymentAmount = new ValueProvider<AdvancePaymentsTreeNode, Double>() {
		@Override
		public Double getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AdvancePayment) {
				AdvancePayment a = (AdvancePayment) model.getObject();
				return a.getAmount();
			}
			return null;
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, Double value) {

		}
		@Override
		public String getPath() {
			return "advancePayment.amount";
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, String> salesInvoiceNo = new ValueProvider<AdvancePaymentsTreeNode, String>() {
		@Override
		public String getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AppliedAdvancePayment) {
				AppliedAdvancePayment a = (AppliedAdvancePayment) model.getObject();
				return a.getSalesInvoice().getGroupAndNo();
			}
			return "";
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, String value) {
			
		}
		@Override
		public String getPath() {
			return "saleInvoice.invoiceNo";
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, Date> dateApplied = new ValueProvider<AdvancePaymentsTreeNode, Date>() {
		@Override
		public Date getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AppliedAdvancePayment) {
				AppliedAdvancePayment a = (AppliedAdvancePayment) model.getObject();
				return a.getSalesInvoice().getDate();
			}
			return null;
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, Date value) {
			
		}
		@Override
		public String getPath() {
			return "dateApplied";
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, Double> appliedAmount = new ValueProvider<AdvancePaymentsTreeNode, Double>() {
		@Override
		public Double getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AppliedAdvancePayment) {
				AppliedAdvancePayment a = (AppliedAdvancePayment) model.getObject();
				return a.getAppliedAmount();
			}
			return null;
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, Double value) {
			
		}
		@Override
		public String getPath() {
			return "appliedAmount";
		}
	};
	
	final ValueProvider<AdvancePaymentsTreeNode, Double> balance = new ValueProvider<AdvancePaymentsTreeNode, Double>() {
		@Override
		public Double getValue(AdvancePaymentsTreeNode model) {
			if (model.getObject() instanceof AdvancePayment) {
				AdvancePayment a = (AdvancePayment) model.getObject();
				return a.getAmount(); // Return the amount instead of balance
			}
			
			if (model.getObject() instanceof AppliedAdvancePayment) {
				AppliedAdvancePayment a = (AppliedAdvancePayment) model.getObject();
				return a.getBalance();
			}
			return null;
		}
		@Override
		public void setValue(AdvancePaymentsTreeNode model, Double value) {
			
		}
		@Override
		public String getPath() {
			return "balance";
		}
	};
	
}